package com.xbn.xbnmall.product.service.impl;

import com.xbn.xbnmall.product.entity.AttrEntity;
import com.xbn.xbnmall.product.service.AttrService;
import com.xbn.xbnmall.product.vo.AttrGroupWithAttrsVo;
import com.xbn.xbnmall.product.vo.SkuItemVo;
import com.xbn.xbnmall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.product.dao.AttrGroupDao;
import com.xbn.xbnmall.product.entity.AttrGroupEntity;
import com.xbn.xbnmall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {

        //如果前端有提交key，那么需要进行模糊查询
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key))
        {
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or()
                        .like("attr_group_name",key);
            });
        }

        //如果catelogId参数不存在(或者==0)，那么查询所有的组
        if(catelogId==0)
        {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
        else {
            //查询指定分类id的分组
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper.eq("catelog_id",catelogId));
            return new PageUtils(page);
        }

    }
    /*
     * 获取某个分类下所有的分组以及这些分组下的所有属性
     * */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查询分组信息
        List<AttrGroupEntity> groups = this.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        //查询分组里的所有的属性
        List<AttrGroupWithAttrsVo> collect = groups.stream().map((item) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            List<AttrEntity> relationAttrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            if(relationAttrs!=null) {
                attrGroupWithAttrsVo.setAttrs(relationAttrs);
            }
            else {
                attrGroupWithAttrsVo.setAttrs(new ArrayList<>());
            }
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /*
    * 根据当前spuId查询出所有和该spuId相关的属性分组信息以及该属性分组下所有的属性和值
    * */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catelogId) {
        List<SpuItemAttrGroupVo>vos= this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catelogId);
        return vos;
    }

}